package com.cays.myresearch.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.cays.myresearch.R;
import com.cays.myresearch.Util.HttpUtil;
import com.cays.myresearch.action.LoginAction;
import com.cays.myresearch.constant.Constant;
import com.cays.myresearch.entity.Classes;
import com.cays.myresearch.entity.Projects;
import com.cays.myresearch.entity.ProjectsAttend;
import com.cays.myresearch.entity.ResponseInfo;
import com.cays.myresearch.entity.Student;
import com.cays.myresearch.entity.Teacher;
import com.cays.myresearch.entity.UserInfo;
import com.cays.myresearch.ui.HeadControlPanel;
import com.google.gson.reflect.TypeToken;

import okhttp3.MultipartBody;

public class ProjectInfoActivity extends AppCompatActivity implements
    HeadControlPanel.HeadPanelCallback,View.OnClickListener{
    private HeadControlPanel projectInfoHeader;
    private TextView projectInfoTitle,projectInfoName,projectInfoType;
    private TextView projectInfoInstruction,projectInfoRequestor;
    private TextView projectInfoLocation,projectInfoStatus;
    private TextView projectInfoCompany,projectInfoTime;
    private Projects intentProject;
    private Button projectInfoAttendBtn;
    private Button projectInfoDeleteBtn;
    private Button projectInfoChangeBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String intentProjectStr=getIntent().getStringExtra("intent_project");
        intentProject=Constant.gson.fromJson(intentProjectStr,
                new TypeToken<Projects>(){}.getType());
        setContentView(R.layout.activity_project_info);
        if (getSupportActionBar()!=null){getSupportActionBar().hide();}
        if(projectInfoHeader==null){
            projectInfoHeader=(HeadControlPanel)findViewById(R.id.project_info_header);
            projectInfoHeader.initHeadPanel();
            projectInfoHeader.setHeadPanelCallback(this);
        }
        projectInfoHeader.setLeftImage(R.drawable.ic_back);
        projectInfoHeader.setMiddleTitle("项目详细信息");
        projectInfoHeader.setRightTitle("");
        initView();
    }
    private void initView(){
        projectInfoTitle = (TextView) findViewById(R.id.project_info_title);
        projectInfoName = (TextView) findViewById(R.id.project_info_name);
        projectInfoType = (TextView) findViewById(R.id.project_info_type);
        projectInfoInstruction = (TextView) findViewById(R.id.project_info_instruction);
        projectInfoRequestor = (TextView) findViewById(R.id.project_info_requestor);
        projectInfoLocation = (TextView) findViewById(R.id.project_info_location);
        projectInfoStatus = (TextView) findViewById(R.id.project_info_status);
        projectInfoCompany = (TextView) findViewById(R.id.project_info_company);
        projectInfoTime = (TextView) findViewById(R.id.project_info_time);

        projectInfoAttendBtn = (Button) findViewById(R.id.project_info_attend_btn);
        projectInfoAttendBtn.setOnClickListener(this);
        projectInfoDeleteBtn = (Button) findViewById(R.id.project_info_delete_btn);
        projectInfoDeleteBtn.setOnClickListener(this);
        projectInfoChangeBtn = (Button) findViewById(R.id.project_info_change_btn);
        projectInfoChangeBtn.setOnClickListener(this);
        if (intentProject!=null){
            projectInfoTitle.setText(intentProject.getTitle());
            projectInfoName.setText(intentProject.getName());
            projectInfoType.setText(intentProject.getType());
            projectInfoInstruction.setText(intentProject.getInstruction());
            projectInfoRequestor.setText(intentProject.getRequestor());
            projectInfoLocation.setText(intentProject.getLocation());
            projectInfoStatus.setText(intentProject.getStatus());
            projectInfoCompany.setText(intentProject.getCompany());
            projectInfoTime.setText(intentProject.getTime());
        }
    }
    @Override
    public void onHeadPanelClick(int itemId) {
        switch (itemId){
            case Constant.btn_left_title:
                finish();
                break;
            case Constant.btn_middle_title:
                break;
            case Constant.btn_right_title:
                break;
            default:break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.project_info_attend_btn:attendProject(intentProject);break;
            case R.id.project_info_delete_btn:deleteProject(intentProject);break;
            case R.id.project_info_change_btn:changeProject(intentProject);break;
            default:break;
        }
    }
    Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what==0){
                Toast.makeText(getApplicationContext(),"未知错误",
                        Toast.LENGTH_SHORT).show();
            }else if (msg.what==2){
                String s=(String)msg.obj;
                ResponseInfo responseInfo=Constant.gson.fromJson(s,
                        new TypeToken<ResponseInfo>(){}.getType());
                Toast.makeText(getApplicationContext(),responseInfo.getResponseInfo(),
                        Toast.LENGTH_SHORT).show();
                if(responseInfo.getResponseCode()==200)finish();
            }else if (msg.what==1){
                String s=(String)msg.obj;
                ResponseInfo responseInfo=Constant.gson.fromJson(s,
                        new TypeToken<ResponseInfo>(){}.getType());
                Toast.makeText(getApplicationContext(),responseInfo.getResponseInfo(),
                        Toast.LENGTH_SHORT).show();
            }else if (msg.what==3){

            }
            return true;
        }
    });
    private void attendProject(Projects projects){
        LoginAction loginAction=new LoginAction();
        if(!loginAction.hasUserInfo(getApplicationContext())){
            Toast.makeText(getApplicationContext(),"未登录",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        UserInfo userInfo=loginAction.getUserInfo(getApplicationContext());
        if(userInfo.getType()!=Constant.user_student){
            Toast.makeText(getApplicationContext(),"不是学生",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        Student student=Constant.gson.fromJson(userInfo.getUser(),
                new TypeToken<Student>(){}.getType());
        ProjectsAttend attend=student.attentProject(projects);
        final String url=Constant.URL+"project/attend";
        final MultipartBody.Builder builder=new MultipartBody.Builder();
        builder.addFormDataPart("attend_project",Constant.gson.toJson(attend));
        HttpUtil.sendHttpRequest(url,builder,handler,1,0);
    }
    private void deleteProject(Projects projects){
        if (!checkPremission(projects))return;
        final String url=Constant.URL+"project/delete";
        final MultipartBody.Builder builder=new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        builder.addFormDataPart("delete_project",Constant.gson.toJson(projects));
        HttpUtil.sendHttpRequest(url,builder,handler,2,0);
    }
    private void changeProject(Projects projects){
        Intent intent=new Intent(ProjectInfoActivity.this,PostProjectActivity.class);
        intent.putExtra("change_project",Constant.gson.toJson(projects));
        startActivity(intent);finish();
    }
    private boolean checkPremission(Projects projects){
        LoginAction loginAction=new LoginAction();
        if(!loginAction.hasUserInfo(getApplicationContext())){
            Toast.makeText(getApplicationContext(),"未登录",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        UserInfo userInfo=loginAction.getUserInfo(getApplicationContext());
        if(userInfo.getType()!=Constant.user_staff){
            Toast.makeText(getApplicationContext(),"不是企业员工",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!userInfo.getName().equals(projects.getName())){
            Toast.makeText(getApplicationContext(),"不是本人",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
